﻿using MediatR;
using W.Purchase.API.Application.Command.SaleCommand;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.SaleCommandHandler
{
    public class SaleOrderIsdelAllCommandHandler : IRequestHandler<SaleOrderIsdelAllCommand, int>
    {
        private readonly ISalesOrdersRepository salesOrdersRepository;

        public SaleOrderIsdelAllCommandHandler(ISalesOrdersRepository salesOrdersRepository)
        {
            this.salesOrdersRepository = salesOrdersRepository;
        }

        public async Task<int> Handle(SaleOrderIsdelAllCommand request, CancellationToken cancellationToken)
        {
            var list = (await salesOrdersRepository.GetAllAsaync()).Where(x => request.ints.Contains(x.SalesOrdersId)).ToList();
            list.ForEach(x =>
            {
                x.IsDelete = true;
                x.DeleteTime = DateTime.Now;
            });
            return await salesOrdersRepository.UpdateRangeAsync(list);
        }
    }
}
